import BreedModel from "../../model/breedModel/breedModel.js"
import { GetConnection, GetResult } from "../../util/db.js"
import { JoinSQLInsertKeys } from "../../util/JoinSQL.js"

class BreedService {
    /**
     * @param {Array<>} modelArray 
     */
    static async InsertBreedList(modelArray) {
        try {
            let valuesArray = []
            for (const item of modelArray) {
                valuesArray.push(new BreedModel(item).GetKeys_Values().values)
            }
            const model = new BreedModel(modelArray[0])
            const conn = await GetConnection()
            let sql = "insert into breed"
            sql += JoinSQLInsertKeys(model.GetKeys_Values().keys)
            const { rows: result } = await GetResult(sql, [valuesArray], conn, true)
            if (result.affectedRows) {
                return result
            } else return false;
        } catch (error) {
            return Promise.reject(error)
        }
    }
    /**
     * 获取全部
     */
    static async GetAllList() {
        try {
            const conn = await GetConnection()
            let sql = "select * from breed"
            const { rows: result } = await GetResult(sql, undefined, conn)
            return result
        } catch (error) {
            return Promise.reject(error)
        }
    }
}

export default BreedService